using System;
using System.IO;
using System.Text;
using UnityEngine;

namespace FRecorder
{
    /// <summary> 录制配置 </summary>
    [Serializable]
    public class RecorderSettings
    {
        public const string SettingsPath = "Library/FRecorderSettings.json";

        /// <summary> 尺寸 </summary>
        public Vector2 size
        {
            get => _size;
            set
            {
                var x = Mathf.Max(1, value.x);
                var y = Mathf.Max(1, value.y);
                _size = new Vector2(x, y);
            }
        }

        [SerializeField] Vector2 _size = new(1280, 720);

        /// <summary> 格式 </summary>
        public int format
        {
            get => _format;
            set => _format = Mathf.Clamp(value, 0, 1);
        }

        [SerializeField] int _format;

        /// <summary> 透明通道 </summary>
        public bool alpha
        {
            get => _alpha;
            set => _alpha = value;
        }

        [SerializeField] bool _alpha;

        /// <summary> 文件名 </summary>
        public string fileName
        {
            get => _fileName;
            set => _fileName = value;
        }

        [SerializeField] string _fileName = "Record";

        /// <summary> 帧率 </summary>
        public int frameRate
        {
            get => _frameRate;
            set => _frameRate = value;
        }

        [SerializeField] int _frameRate = 1;

        /// <summary> 开始帧 </summary>
        public int startFrame
        {
            get => _startFrame;
            set => _startFrame = Mathf.Max(0, value);
        }
        [SerializeField] int _startFrame;

        /// <summary> 结束帧 </summary>
        public int endFrame
        {
            get => _endFrame;
            set => _endFrame = value > startFrame ? value : startFrame + 1;
        }

        [SerializeField] int _endFrame = 1;

        public string savePath
        {
            get
            {
                if (!Directory.Exists(_savePath))
                    _savePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop).Replace('\\', '/');
                return _savePath;
            }
            set
            {
                if (Directory.Exists(value))
                    _savePath = value;
            }
        }

        [SerializeField] string _savePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop).Replace('\\', '/');

        /// <summary> 保存配置 </summary>
        public static void SaveSettings(RecorderSettings recorderSettings)
        {
            // settings 配置

            var jsonStr = JsonUtility.ToJson(recorderSettings, true);

            if (!File.Exists(SettingsPath))
            {
                using StreamWriter sw = new StreamWriter(SettingsPath, false, Encoding.UTF8);
                sw.Write(jsonStr);
                sw.Close();
            }
            else
            {
                File.WriteAllText(SettingsPath, jsonStr);
            }
        }

        /// <summary> 读取或创建 </summary>
        public static RecorderSettings ReadOrCreate()
        {
            RecorderSettings recorderSettings = null;

            if (File.Exists(SettingsPath))
            {
                var jsonStr = File.ReadAllText(SettingsPath);
                try
                {
                    recorderSettings = JsonUtility.FromJson<RecorderSettings>(jsonStr);
                }
                catch
                {
                    // 忽略异常
                }
            }

            if (recorderSettings == null)
            {
                recorderSettings = new RecorderSettings();
                SaveSettings(recorderSettings);
            }

            return recorderSettings;
        }
    }
}